var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var del = require('del');
var runSequence = require('run-sequence');
var assetRev = require('gulp-asset-rev');
var livereload = require('gulp-livereload');

function gulpScripts() {
    return gulp.src(['src/**/*.js']) //源文件下的所有js
        .pipe(assetRev())                    //配置版本号
        .pipe($.uglify())                    //进行压缩，如果需要合并也可加上合并的代码
        .pipe(gulp.dest("dist"));//复制到目标文件路径
}

function gulpStyles() {
    return gulp.src(['src/**/*.css'])
        .pipe(assetRev())
        .pipe($.minifyCss())
        .pipe(gulp.dest("dist"));
}

function gulpImages() {
    return gulp.src(['src/**/images/*'])
        .pipe(gulp.dest("dist"));   //复制所有图片到目标文件夹
}

function gulpRevHtml() {
    gulp.src(['src/*.html', 'src/**/*.html'])   //源文件下面是所有html
        .pipe(assetRev())                                       //配置引用的js和css文件，需要的话也可以用minifyHtml压缩html文件
        .pipe(gulp.dest('dist'));                   //打包到目标文件夹路径下面
}

var pkg = require('./package.json');
var srcRoot = "src";
var srcPath = {
    html: srcRoot + "/" + "html/**",
    script: srcRoot + "/" + "js/**/*.js",
    css: srcRoot + "/" + "css/*.css",
    images: srcRoot + "/" + "images/**"
}
//livereload
gulp.task('live', function () {
    livereload.listen();
    gulp.watch([srcPath.html, srcPath.css, srcPath.script]).on('change', livereload.changed);
})

gulp.task('app_scripts', function(){
    gulpScripts();
});
gulp.task('app_styles', function(){
    gulpStyles();
});
gulp.task('app_images',function(){
    gulpImages();
});
gulp.task('app_rev', ['app_styles', 'app_scripts'], function(){
    gulpRevHtml();
});
gulp.task('clean', del.bind(null, ['dist'], {
    force: true
}));
gulp.task("build", function() {
    runSequence('clean', ["app_images", "app_rev"]);
});
